#include<iostream>
#include<pthread.h>
#include<vector>
#include<time.h>
#include<functional>
#include<string>
#include<unistd.h>
using func_t =std::function<void()>;
class ThreadData
{
    public:
    ThreadData(std::string threadname,uint64_t ctime,func_t func):_threadname(threadname),_ctime(ctime),_func(func)
    {}
    public:
    std::string _threadname;
    uint64_t _ctime;
    func_t _func;
};
const int ThreadNum=5;

std::vector<pthread_t> pthreads;
void*ThreadRoutine(void*args)
{
    int a=10;
    ThreadData*td=static_cast<ThreadData*>(args);
    int cnt=5;
    while(cnt--)
    {
        std::cout<<td->_threadname<<" "<<td->_ctime<<std::endl;
        td->_func();
        sleep(1);
    }
    pthread_exit(nullptr);
}
std::string toHex(pthread_t tid)
{
    char id[64];
    snprintf(id,sizeof(tid),"0x%x",tid);
    return id;
}
void Print()
{
    std::cout<<"我是td的一部分"<<std::endl;
    sleep(1);
}
int main(int argc,char*argv[])
{
    char threadname[64];
    pthread_t tid;
    for(int i=1;i<=ThreadNum;i++)
    {
        snprintf(threadname,sizeof(threadname),"%s-%u","Thread",i);
        
        ThreadData*td=new ThreadData(threadname,(uint64_t)time(nullptr),Print);
        pthread_create(&tid,nullptr,ThreadRoutine,td);
        
    }
    int n=pthread_join(tid,nullptr);
    std::cout<<"Thread Done ,id is"<<toHex(tid)<<std::endl;
    
    //     while (true)
    // {
    //     std::cout << "main thread" << std::endl;
    //     sleep(3);
    // }
}